// page_two/shouadd/shouadd.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '收费标准',
    buttonClicked: false,
    typeindex: 0,
    // typelist: ['物业服务费', '装修押金', '装修服务费', '代收费用', 'IC卡类收费'],
    typelist: [], //分类
    calssid: '', //分类ID
    type: '请选择分类',
    xmindex: 0,
    // xmlist: ['项目名称', '项目名称', '项目名称'],
    xmlist: [], //项目
    itemid: '', //项目ID
    xm: '请选择项目名称',
    unitindex: 0,
    unitlist: [], //单位
    valuaid: '', //计价单位ID
    unit: '请选择计价单位',
    standard: '',
    photo: [],
    wuphoto: '',
    type_mold: 20,
    pid: 0
  },
  getType(type_mold, pid, list) {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Besall/public_class_options', {
      type_mold: type_mold,
      pid: pid
    }, 'POST').then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        this.setData({
          [list]: res.data.data
        })
        console.log(this.data.typelist);
        console.log(this.data.xmlist);
        console.log(this.data.unitlist);
      }
    })
  },
  // 分类
  type(e) {
    let index = e.detail.value
    this.setData({
      type: this.data.typelist[index].name,
      calssid: this.data.typelist[index].id,
      pid: this.data.typelist[index].id,
      xmindex: 0,
      xm: '请选择项目名称',
      unitindex: 0,
      unit: '请选择计价单位',
    });
    this.getType(21, this.data.pid, 'xmlist');
  },
  // 项目名称
  xm(e) {
    let index = e.detail.value
    this.setData({
      xm: this.data.xmlist[index].name,
      itemid: this.data.xmlist[index].id,
      // pid: this.data.xmlist[index].id,
      unitindex: 0,
      unit: '请选择计价单位',
    });
    this.getType(22, this.data.pid, 'unitlist');
  },
  // 计价单位
  unit(e) {
    let index = e.detail.value
    this.setData({
      unit: this.data.unitlist[index].name,
      valuaid: this.data.unitlist[index].id
    });
  },
  // 收费标准
  standard(e) {
    this.setData({
      standard: e.detail.value
    })
  },
  // 上传
  chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.duoupimg(tempFilePaths[i]).then(res => {
                wx.showLoading({
                  title: '上传图片中...',
                });
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: wuphoto
                });
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading();
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
  // 差掉
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    var dd = wuphoto.split(",")
    console.log(dd)
    photo.splice(index, 1)
    dd.splice(index, 1)
    var rr = dd.join(",")
    this.setData({
      photo: photo,
      wuphoto: rr
    })
  },
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  // 验证
  ti() {
    if (this.data.type == '请选择分类') {
      wx.showToast({
        title: '请选择分类',
        icon: 'none'
      })
    } else if (this.data.xm == '请选择项目名称') {
      wx.showToast({
        title: '请选择项目名称',
        icon: 'none'
      })
    } else if (this.data.unit == '请选择计价单位') {
      wx.showToast({
        title: '请选择计价单位',
        icon: 'none'
      })
    // } else if (this.data.photo.length == 0) {
    //   wx.showToast({
    //     title: '请上传图片',
    //     icon: 'none'
    //   })
    } else {
      this.que()
    }
  },
  // 添加收费标准
  que() {
    let that = this
    that.setData({
      buttonClicked: true,
    })
    this.myRequest(yuming + 'shop/shop_chargesfees_add', {
      shopId: wx.getStorageSync('shopId'),
      calssid: that.data.calssid,
      itemid: that.data.itemid,
      valuaid: that.data.valuaid,
      chargesfees: that.data.standard,
      // image: that.data.wuphoto
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(function () {
          wx.navigateBack({
            delta: 0
          })
        }, 1500)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5
    })
    this.getType(this.data.type_mold, this.data.pid, 'typelist');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})